<?php
defined('SECUR_CHECK') or die("Security error");
$site->set_templates(array("head.tpl", "register.tpl", "footer.tpl"));
$register = false;
$error_msg = '';
$error = false;
if(isset ($_POST['register']))
{
    if(!$site->check_email($_POST['email']))
    {
        $error_msg .= "E-mail is invalid<br />";
        $error = true;
    }
    if(!ctype_alnum(str_replace(" ", "", $_POST['name'])))
    {
        $error_msg .= "Name is invalid<br />";
        $error = true;
    }
    if($_POST['password'] == '')
    {
        $error_msg .= "Password is invalid<br />";
        $error = true;
    }
    if($user->user_exists($_POST['email']))
    {
        $error_msg .= "User already exists<br />";
        $error = true;
    }
    if(!$error && $user->new == true)
    {
        // Save new user
        $user->email = $_POST['email'];
        $user->password = md5($_POST['password']);
        $user->profile[0]['prof_fullname'] = $_POST['name'];
        if($user->save())
        {
            $register = true;
        }
        else
        {
            $error = true;
            $error_msg .= "Error occured while registering new user<br />";
        }
    }
}
$smarty->assign("ERROR_MSG", $error_msg);
$smarty->assign("REGISTER", $register);
?>
